//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FaceValueAccrualClaim.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/Bond.h>
#include <gen/QL/Instruments/Claim.h>
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (Cephei::QL::Instruments::IBond^ referenceSecurity) : CClaim(CFaceValueAccrualClaim::typeid)
{
    CBond^ _CreferenceSecurity;
    try
    {
#ifdef HANDLE
        _phFaceValueAccrualClaim = NULL;
#endif
        _CreferenceSecurity = safe_cast<CBond^> (referenceSecurity);
        _CreferenceSecurity->Lock();
        boost::shared_ptr<QuantLib::Bond>& _referenceSecurity = static_cast<boost::shared_ptr<QuantLib::Bond>&> (_CreferenceSecurity->GetShared ()); 
        _ppFaceValueAccrualClaim = new boost::shared_ptr<QuantLib::FaceValueAccrualClaim> (new QuantLib::FaceValueAccrualClaim ( _referenceSecurity ));
        SetClaim (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueAccrualClaim));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CreferenceSecurity != nullptr) _CreferenceSecurity->Unlock();
    }
}
Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (boost::shared_ptr<QuantLib::FaceValueAccrualClaim>& childNative, Object^ owner) : CClaim(CFaceValueAccrualClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueAccrualClaim = NULL;
#endif
	_ppFaceValueAccrualClaim = &childNative;
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueAccrualClaim));
}
Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (QuantLib::FaceValueAccrualClaim& childNative, Object^ owner) : CClaim(CFaceValueAccrualClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueAccrualClaim = NULL;
#endif
	_ppFaceValueAccrualClaim = new boost::shared_ptr<QuantLib::FaceValueAccrualClaim> (&childNative);
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueAccrualClaim));
    _FaceValueAccrualClaimOwner = owner;
    _ClaimOwner = owner;
}

Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (CFaceValueAccrualClaim^ copy) : CClaim(CFaceValueAccrualClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueAccrualClaim = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFaceValueAccrualClaim = new boost::shared_ptr<QuantLib::FaceValueAccrualClaim> (copy->GetShared());
        _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueAccrualClaim));
    }
}
Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (PLATFORM::Type^ t) : CClaim(CFaceValueAccrualClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueAccrualClaim = NULL;
#endif
	if (!t->IsSubclassOf(CFaceValueAccrualClaim::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (QuantLib::Handle<QuantLib::FaceValueAccrualClaim>& childNative, Object^ owner)  : CClaim(CFaceValueAccrualClaim::typeid)
{
	_phFaceValueAccrualClaim = &childNative;
	_ppFaceValueAccrualClaim = &static_cast<boost::shared_ptr<QuantLib::FaceValueAccrualClaim>>(childNative.currentLink());
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueAccrualClaim));
    _FaceValueAccrualClaimOwner = owner;
}
Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (QuantLib::Handle<QuantLib::FaceValueAccrualClaim> childNative)  : CClaim(CFaceValueAccrualClaim::typeid)
{
	_phFaceValueAccrualClaim = &childNative;
	_ppFaceValueAccrualClaim = &static_cast<boost::shared_ptr<QuantLib::FaceValueAccrualClaim>>(childNative.currentLink());
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueAccrualClaim));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CFaceValueAccrualClaim::CFaceValueAccrualClaim (QuantLib::FaceValueAccrualClaim childNative)  : CClaim(CFaceValueAccrualClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueAccrualClaim = NULL;
#endif
	_ppFaceValueAccrualClaim = new boost::shared_ptr<QuantLib::FaceValueAccrualClaim> (new QuantLib::FaceValueAccrualClaim (childNative));
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueAccrualClaim));
}
#endif

Cephei::QL::Instruments::CFaceValueAccrualClaim::~CFaceValueAccrualClaim ()
{
    if (_ppFaceValueAccrualClaim != NULL)
    {
	    delete _ppFaceValueAccrualClaim;
        _ppFaceValueAccrualClaim = NULL;
    }
}
Cephei::QL::Instruments::CFaceValueAccrualClaim::!CFaceValueAccrualClaim ()
{
    if (_ppFaceValueAccrualClaim != NULL)
    {
	    delete _ppFaceValueAccrualClaim;
    }
}
QuantLib::FaceValueAccrualClaim& Cephei::QL::Instruments::CFaceValueAccrualClaim::GetReference ()
{
    if (_ppFaceValueAccrualClaim == NULL) throw REFNEW NativeNullException ();
	return **_ppFaceValueAccrualClaim;
}
boost::shared_ptr<QuantLib::FaceValueAccrualClaim>& Cephei::QL::Instruments::CFaceValueAccrualClaim::GetShared ()
{
    if (_ppFaceValueAccrualClaim == NULL) throw REFNEW NativeNullException ();
	return *_ppFaceValueAccrualClaim;
}
QuantLib::FaceValueAccrualClaim* Cephei::QL::Instruments::CFaceValueAccrualClaim::GetPointer ()
{
    if (_ppFaceValueAccrualClaim == NULL) throw REFNEW NativeNullException ();
	return &**_ppFaceValueAccrualClaim;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FaceValueAccrualClaim>& Cephei::QL::Instruments::CFaceValueAccrualClaim::GetHandle ()
{
	if (_phFaceValueAccrualClaim == NULL)
	{
		_phFaceValueAccrualClaim = new Handle<QuantLib::FaceValueAccrualClaim> (*_ppFaceValueAccrualClaim);
	}
	return *_phFaceValueAccrualClaim;
}
#endif
bool Cephei::QL::Instruments::CFaceValueAccrualClaim::HasNative () 
{
	return (_ppFaceValueAccrualClaim != NULL);
}

Double Cephei::QL::Instruments::CFaceValueAccrualClaim::Amount (DateTime d, Double notional, Double recoveryRate)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional); //a
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFaceValueAccrualClaim)->amount ( _d,  _notional,  _recoveryRate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IFaceValueAccrualClaim^ Cephei::QL::Instruments::CFaceValueAccrualClaim_Factory::Create (Cephei::QL::Instruments::IBond^ referenceSecurity)
{
    return REFNEW CFaceValueAccrualClaim ( referenceSecurity);
}
